package cn.fastoo.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Date;


/**
 * 短信息内容辅助类
 */
public class MessageHelper {

    private static int sbcLength = 70; // 中文短短信，70一条计费
    private static int mSbcLength = 67; // 中文长短信，67一条
    private static int dbcLength = 140; // 全ascii，140一条
    private static int sDbcLength = 134; // 全ascii，134一条

    // 计算短信计费条数
    public static int getSmsLength(String msg, String countryCode, String sign) {
        if (StringUtils.isBlank(msg)) {
            return 0;
        }

        String content = msg;
        int strLen = content.length();
        int smsLen = 1;

        int s1 = sbcLength;
        int s2 = mSbcLength;

        if (countryCode.equals("CN")) { //countryCode号码所对应的区号，计算签名
            content += "【" + sign + "】";
            strLen = content.length();
        } else {
            s1 = dbcLength;
            s2 = sDbcLength;

            String special = "";   //特殊字符
            //判断是否有全角字符
            for (int i = 0; i < strLen; i++) {
                char c = content.charAt(i);
                String s = Character.toString(c);
                if (s.getBytes().length >= 2 || special.indexOf(s) > -1) {
                    // 含全角
                    s1 = sbcLength;
                    s2 = mSbcLength;
                    break;
                }
            }
        }

        // 长、短短信条数统计
        if (strLen > s1) {
            smsLen = (int) Math.floor(strLen / s2);
            if (strLen % s2 > 0) {
                smsLen++;
            }
        }

        return smsLen;
    }

    private static int accessId = 0;
    public static synchronized String getSequenceId(long timestamp) {
        long localMsgId = timestamp + getAccessId();
        return Long.toHexString(localMsgId);
    }

    public static long getAccessId() {
        accessId++;
        if (accessId > 900) {
            accessId = 1;
        }
        return accessId;
    }

    public static String getErrorStr(String mterrorcode) {
        String error = "";
        if ("0101".equals(mterrorcode)||"0100".equals(mterrorcode)) {
            error = "请求命令错误";
        }else if ("0101".equals(mterrorcode)) {
            error = "请求命令错误";
        }else if ("0111".equals(mterrorcode)) {
            error = "发送内容为空";
        }else if ("0104".equals(mterrorcode)) {
            error = "账号信息错误";
        }else if ("0107".equals(mterrorcode)) {
            error = "余额不足";
        }else if ("0108".equals(mterrorcode)) {
            error = "非白名单IP访问";
        }else if ("0110".equals(mterrorcode)) {
            error = "目标地址超过100个或没有发送号码";
        }else if ("0112".equals(mterrorcode)) {
            error = "模板验证不通过";
        }else if ("0600".equals(mterrorcode)) {
            error = "处理下行短信发生异常";
        }
        return error;
    }

    public static void main(String[] args) {
        try {
            System.out.println(getSequenceId(new Date().getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
